#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>


#define str "hello world"

int main( int argc, char **argv)
{
	int				fd = -1;
	off_t			offt = -1;
	int				rv = -1;
	char			buf[1024];

	fd =  open("test.txt", O_RDWR|O_CREAT|O_APPEND,0666);    // 打开文件
	if(fd == -1)
	{
		printf(" open error : %s \n", strerror(errno));
		return -1;
	}
	printf("open succed fd[%d] \n " , fd);

	rv = write(fd, str, sizeof(str));     //写入字符串
	if( rv == -1)
	{
		printf(" write error : %s \n", strerror(errno));
		goto clean;
	}	

	printf("write succed rv[%d] \n", rv );
	
	rv = lseek(fd, 0, SEEK_SET);       // 将文件偏移量设置为文件开头
	if(rv == -1)
	{
		printf("lseek error :%s \n", strerror(errno));
		goto clean;
	}
	printf("lseek succed rv[%d] \n ", rv);
	
	rv = read(fd, buf, sizeof(buf));     // 读取全部内容，假设文件没有超过1024
	if(rv == -1)
	{
		printf("read error : %s \n",strerror(errno));
		goto clean;
	}
	printf("read succed rv[%d],: %s \n", rv , buf);    // 打印读取到的内容

clean:
	close(fd);


	return 0;

}





